package com.wgq.JDBC;

import com.wgq.IO.GetPropertiesFile;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class JdbcDome {

    private static HashMap getJDBCResoure(){
        GetPropertiesFile gp = new GetPropertiesFile();
        ArrayList<Map> list = new ArrayList();
        HashMap map = new HashMap();

        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取数据库连接
            try {
                Connection conn = DriverManager.getConnection(
                        gp.getPropertiesFileOneList("URL"),
                        gp.getPropertiesFileOneList("USER"),
                        gp.getPropertiesFileOneList("PASSWORD"));
                PreparedStatement ps= conn.prepareStatement(gp.getPropertiesFileOneList("SQL"));
                ResultSet rs = ps.executeQuery();
                while (rs.next()){
                    //System.out.println("id:"+rs.getInt("id")+"\tname:"+rs.getString("name"));
                    map.put(rs.getInt("id"),rs.getString("name"));
                    list.add(map);

                }
                ps.close();
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return  map;
    }
    public static void main(String[] args) {

        Iterator<Map.Entry<Object,String>> it = JdbcDome.getJDBCResoure().entrySet().iterator();
        while (it.hasNext()){
            Map.Entry<Object,String> entry = it.next();
            System.out.println(entry.getKey()+"\t"+entry.getValue());
        }
    }
}
